package com.atguigu.edu.controller;


import com.atguigu.common.util.R;
import com.atguigu.edu.entity.EduChapter;
import com.atguigu.edu.entity.vo.EduChapterVo;
import com.atguigu.edu.entity.vo.EduCourseVo;
import com.atguigu.edu.service.EduChapterService;
import com.atguigu.edu.service.EduCourseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 *
 * 由于目前不允许删除含有小节的章节
 * 因此 删除章节 控制器不需要加入删除 阿里云视频的功能
 * </p>
 *
 * @author alexzshl
 * @since 2021-05-04
 */
@RestController
@RequestMapping("/api/edu/chapter")
@CrossOrigin
public class EduChapterController {

    @Resource
    private EduCourseService eduCourseService;

    @Resource
    private EduChapterService eduChapterService;

    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVoList(@PathVariable("courseId") String courseId) {
        List<EduChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        EduCourseVo courseInfo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("items", list).data("data", list).data("result", list)
                .data("courseInfo", courseInfo);
    }

    @GetMapping("/getChapter/{chapterId}")
    public R addChapter(@PathVariable("chapterId") String chapterId) {
        EduChapter chapter = eduChapterService.getById(chapterId);
        return R.ok().data("data", chapter);
    }

    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter chapter) {
        eduChapterService.addChapter(chapter);
        return R.ok();
    }

    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter chapter) {
        eduChapterService.updateChapter(chapter);
        return R.ok();
    }

    /**
     * 删除 课时、小节
     * 同时需要释放云端视频资源
     * @param chapterId
     * @return
     */
    @PostMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable("chapterId") String chapterId) {
        eduChapterService.deleteChapter(chapterId);
        return R.ok();
    }
}

